// P1KDlg.cpp : implementation file
//

#include "stdafx.h"
#include "YL_DEMO_VOIP.h"
#include "P1KDlg.h"
#include "YL_DEMO_VOIPDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CP1KDlg dialog


CP1KDlg::CP1KDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CP1KDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CP1KDlg)
	m_strLCDText = _T("");
	//}}AFX_DATA_INIT
	m_pParent = pParent;
}


void CP1KDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CP1KDlg)
	DDX_Text(pDX, IDC_LCDTEXT, m_strLCDText);
	
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CP1KDlg, CDialog)
	//{{AFX_MSG_MAP(CP1KDlg)
	ON_BN_CLICKED(IDC_SETRING, OnSetring)
	ON_BN_CLICKED(IDC_SETTONE, OnSettone)
	ON_BN_CLICKED(IDC_SETLCD, OnSetlcd)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CP1KDlg message handlers

void CP1KDlg::OnSetring() 
{
	static BOOL bRing = TRUE;
	static int iIndex = 0;

	YL_DeviceIoControl(YL_IOCTL_SET_RING, (LPVOID)bRing, (LPVOID)iIndex);

	if ( bRing )
	{
		CString strText = _T("");
		strText.Format( _T("START_RING %d"), iIndex );
		GetDlgItem(IDC_RINGSTATE)->SetWindowText( strText );
		iIndex = ++iIndex%10;
	}
	else
		GetDlgItem(IDC_RINGSTATE)->SetWindowText( _T("STOP_RING") );

	bRing = ! bRing;
}

void CP1KDlg::OnSettone() 
{
	static int eType = PHONE_TONE_SILENCE;

	YL_DeviceIoControl(YL_IOCTL_SET_TONE, (LPVOID)eType);
	
	switch ( eType )
	{
	case PHONE_TONE_UNKNOW:
		GetDlgItem(IDC_TONESTATE)->SetWindowText( _T("TONE_UNKNOW") );
		break;
	case PHONE_TONE_SILENCE:
		GetDlgItem(IDC_TONESTATE)->SetWindowText( _T("TONE_SILENCE") );
		break;
	case PHONE_TONE_MUTE:
		GetDlgItem(IDC_TONESTATE)->SetWindowText( _T("TONE_MUTE") );
		break;
	case PHONE_TONE_DIAL:
		GetDlgItem(IDC_TONESTATE)->SetWindowText( _T("TONE_DIAL") );
		break;
	case PHONE_TONE_CONNECTING:
		GetDlgItem(IDC_TONESTATE)->SetWindowText( _T("TONE_CONNECTING") );
		break;
	case PHONE_TONE_RINGBACK:
		GetDlgItem(IDC_TONESTATE)->SetWindowText( _T("TONE_RINGBACK") );
		break;
	case PHONE_TONE_BUSY:
		GetDlgItem(IDC_TONESTATE)->SetWindowText( _T("TONE_BUSY") );
		break;
	default:
		break;
	}
	
	eType++;
	
	if ( eType > PHONE_TONE_BUSY )
		eType = PHONE_TONE_SILENCE;
}

void CP1KDlg::OnSetlcd() 
{
	UpdateData( TRUE );

/*
	if ( m_strLCDText.IsEmpty() )
	{
		pParent->m_XUSB.P1KSetLCDText( LCD_OFFHOOK_FREE, 0 );
	}
	else
	{
		pParent->m_XUSB.P1KSetLCDText( LCD_TEXT_RIGHT, (LPARAM)(LPCTSTR)m_strLCDText );
	}
*/
	YL_DeviceIoControl(YL_IOCTL_SET_P1KLCDTEXT, (LPVOID)(LPCTSTR)m_strLCDText);
}

